;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var router = require("../hash/pageRouter");
	var validate = require("validate");
	var css_collection = require("./commodityPriceUnchecked.css");
	var html_add = require("./commodityPriceGather-add.html");
	var fileUuid;
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化

	var init = function(num) {
		inquireBulkstockCategoryPageList();
		$(".table-section").removeClass("none");
		riseAndFallColor();
		inquireBulkstockGatherList();
		initEvent();
		router.init();
	};

	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		$("#main").on("click", "#uploadGatherPrice", function() {
			uploadPrice();
			$("#add_gather_dialog").on("change", "#priceGather", function() {
				uploadFile();
			});
		});

		$("#commodityPriceUnchecked1").on("click", ".table_delete_btn", function() {
			var $this = $(this);
			dialog.showConfirmDialog("商品价格汇总删除", "是否确认删除该条数据？", function() {
				deleteBulkstockGather($this.parents("tr").attr("gatherId"));
			});
		});

		$(".table-search-button-1").on("click", function() {
			pageIndex = 1;
			pageSize = 10;
			totalPageCount = 1;
			totalRecordCount = 0; //分页初始化
			inquireBulkstockGatherList();
		});

	};

	var riseAndFallColor = function() {
		var riseAndFall = $(".riseAndFall");
		for (var i = 0, j = riseAndFall.length; i < j; i++) {
			if (riseAndFall[i].innerHTML.charAt(0) == "-") {
				$(riseAndFall[i]).css("color", "#33A55E");
			} else if (riseAndFall[i].innerHTML.charAt(0) == "+") {
				$(riseAndFall[i]).css("color", "#F1592A");
			}
		}
	};

	//上传附件
	var uploadFile = function() {
		if ($("#priceMaterial").val() != "") {
			$.ajaxFileUpload({
				url: "/finance/api/v1.0/uploadFile.json", //你处理上传文件的服务端
				secureuri: false,
				fileElementId: 'priceGather',
				dataType: 'json',
				success: function(data) {
					fileUuid = data.data[0].fileUuid;
					var fileName = data.data[0].fileName;
					$(".priceGatherValue").val(fileName);
					$(".priceMaterial").html(fileName);
				},
				complete: function(xml) {
					$("#uploadImg").unbind().on('change', function() {
						uploadFile();
					});
				},
				error: function(data, status, e) {
					if (data.msg == null) {
						base.alert(2, "文件太大无法上传");
					} else {
						base.alert(2, data.msg);
					}
				}
			});
		} else {
			base.alert(3, "请选择文件！");
		}
	};

	var uploadPrice = function() {
		$("#popdialog").html(html_add);
		dialog.showDialog($("#add_gather_dialog"));
		$("#add_gather_dialog").on("click", "#surePriceGather", function() {
			if ($(".priceGatherValue").val() == null || $(".priceGatherValue").val() == "") {
				base.alert(2, "请选择文件");
				return;
			} else {
				importBulkstockGather();
			}
		});
	};

	//货物类别下拉框
	var inquireBulkstockCategoryPageList = function() {
		base.fnAjax({
			action: "inquireBulkstockCategoryPageList.json",
			oData: {
				pageSize: 1000,
				activeState: true
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#commodityPriceUnchecked1 .categoryTpl").html();
					var html = juicer(tpl, data);
					$("#commodityPriceUnchecked1 .bulkstockSelect").html(html);
				} else {
					base.alert(2, data.message);
				}
			}
		});
	};

	var importBulkstockGather = function() {
		base.fnAjax({
			action: "importBulkstockGather.json",
			oData: {
				fileUuid: fileUuid
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "上传新价格成功");
					dialog.hideDialog($("#add_gather_dialog"));
					base.openUrl("commodityCategory.html#commodityPriceManage/commodityPriceUnchecked.html");
				} else {
					base.alert(2, data.message);
				}
			}
		});
	};

	//同意发布
	var issueBulkstockGather = function(gatherId) {
		base.fnAjax({
			action: "issueBulkstockGather.json",
			oData: {
				gatherId: gatherId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {} else {
					base.alert(2, data.message);
				}
			}
		});
	};

	//删除商品价格汇总
	var deleteBulkstockGather = function(gatherId) {
		base.fnAjax({
			action: "disableBulkstockGather.json",
			oData: {
				gatherId: gatherId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "删除成功！");
					router.route("commodityPriceManage/commodityPriceUnchecked.html");
				} else {
					base.alert(2, data.message);
				}
			}
		});
	};

	//查询价格汇总列表
	var inquireBulkstockGatherList = function() {
		base.fnAjax({
			action: "inquireBulkstockGatherList.json",
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				auditState: 0
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#uncheckedTpl").html();
					var html = juicer(tpl, data);
					$("#uncheckedBody").html(html);

					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl($("#unchecked_pagerCon"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireBulkstockGatherList();
						});
					$(".agreeIssue").unbind('click').click(function() {
						issueBulkstockGather($(this).parent().parent().parent().attr("gatherId"));
						$(this).parent().parent().parent().remove();
					});
				} else {
					base.alert(2, data.message);
				}
			}
		});
	};

	return {
		init: init
	};
});